package com.strawcom.charmring.imagebrowser;


import java.util.List;

import org.kymjs.kjframe.KJBitmap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.strawcom.charmring.R;
import com.strawcom.charmring.imagebrowser.MyImageView.OnMeasureListener;
import com.strawcom.charmring.imagebrowser.NativeImageLoader.NativeImageCallBack;

public class ChildAdapter extends BaseAdapter {
	private Point mPoint = new Point(0, 0);//用来封装ImageView的宽和高的对象
	private GridView mGridView;
	private List<String> list;
	protected LayoutInflater mInflater;

	public ChildAdapter(Context context, List<String> list, GridView mGridView) {
		this.list = list;
		this.mGridView = mGridView;	
		mInflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}


	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		final String path = list.get(position);
		
		if(convertView == null){
			convertView = mInflater.inflate(R.layout.grid_child_item, null);
			viewHolder = new ViewHolder();
			viewHolder.mImageView = (MyImageView) convertView.findViewById(R.id.child_image);
			
//			//用来监听ImageView的宽和高
//			viewHolder.mImageView.setOnMeasureListener(new OnMeasureListener() {
//				
//				@Override
//				public void onMeasureSize(int width, int height) {
//					mPoint.set(width, height);
//					
//					KJBitmap kjb = KJBitmap.create();
//					kjb.display(viewHolder.mImageView, path, mPoint.x, mPoint.y);
//				}
//			});
			
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
//			viewHolder.mImageView.setImageResource(R.drawable.friends_sends_pictures_no);
		}
//		viewHolder.mImageView.setTag(path);
		
		//利用NativeImageLoader类加载本地图片
//		Bitmap bitmap = NativeImageLoader.getInstance().loadNativeImage(path, mPoint, new NativeImageCallBack() {
//			
//			@Override
//			public void onImageLoader(Bitmap bitmap, String path) {
//				ImageView mImageView = (ImageView) mGridView.findViewWithTag(path);
//				if(bitmap != null && mImageView != null){
//					mImageView.setImageBitmap(bitmap);
//				}
//			}
//		});
		
//		if(bitmap != null){
//			viewHolder.mImageView.setImageBitmap(bitmap);
//		}else{
//			viewHolder.mImageView.setImageResource(R.drawable.friends_sends_pictures_no);
//		}
		
//		KJBitmap kjb = KJBitmap.create();
//		kjb.display(viewHolder.mImageView, path, mPoint.x, mPoint.y);
		
		
		Glide.with(parent.getContext()).load(list.get(position))
		.placeholder(R.drawable.friends_sends_pictures_no)
		.centerCrop().crossFade().into(viewHolder.mImageView);
		
		return convertView;
	}
	
	public static class ViewHolder{
		public MyImageView mImageView;
	}

}
